$(function () {
    var $toggleFilter = $('#filter-toggle');
    var $scheduleFilter = $('#schedule_filter');
    $toggleFilter.on('click', function(){
        $scheduleFilter.slideToggle(200);
    });
});

$(function() {
    var $people = $('[name="people_type"]');
    $people.on('change',function() {
        $("#personal_select_container").toggle(200);
    });
});

$(function() {
    var $time = $('[name="time_type"]');

    $time.on('change',function() {
        if($time.filter(':checked').val() == 'day'){
            $('#day').datepicker('open');
        }
    });
});

var app = angular.module('scheduleList', []).controller('listController', function ($scope, $http) {
    if (sessionStorage.getItem('schedule.type') == null) {
        $scope.scheduleType = 'survey';
        $scope.schedulePeople = 'team';
        $scope.scheduleDesc = '调研';
        $scope.startTime = '';
        $scope.endTime = '';
    } else {
        $scope.scheduleType = sessionStorage.getItem('schedule.type');
        $scope.schedulePeople = sessionStorage.getItem('schedule.people');
        $scope.scheduleDesc = sessionStorage.getItem('schedule.desc');
        $scope.startTime = sessionStorage.getItem('schedule.stime');
        $scope.endTime = sessionStorage.getItem('schedule.etime');

        var $type = $('[name="schedule_type"]');
        $type.parent().removeClass('am-active');
        $type.filter('[value="'+$scope.scheduleType+'"]').parent().addClass('am-active');
    }

    $scope.queryList = function () {
        sessionStorage.setItem('schedule.type', $scope.scheduleType);
        sessionStorage.setItem('schedule.people', $scope.schedulePeople);
        sessionStorage.setItem('schedule.desc', $scope.scheduleDesc);
        sessionStorage.setItem('schedule.stime', $scope.startTime);
        sessionStorage.setItem('schedule.etime', $scope.endTime);
        $scope.listData = [];
        var url = '/smtool_project/wxapp/api/schedule/list?type=' + $scope.scheduleType;
        if($scope.schedulePeople == 'personal') {
            url += '&createUid=' + $('#personal_select option:selected').val();
        }
        if($scope.startTime != '' && $scope.endTime != '') {
            url += '&startTime=' + $scope.startTime;
            url += '&finishTime=' + $scope.endTime;
        }
        $.AMUI.progress.start();
        $http.get(url).success(function (response) {
            if (response.code == 200) {
                $scope.listData = response.obj;
            } else {
                $scope.listData = [];
            }
            $.AMUI.progress.done();
        }).error(function (err) {
            $scope.listData = [];
            $.AMUI.progress.done();
        })
    };

    $scope.queryList();

    $('#personal_select').on('change', function () {
        $scope.queryList();
    });

    $scope.changeDate = function (type) {
        var date = new Date();
        if(type == 'all') {
            $scope.startTime = '';
            $scope.endTime = '';
        } else if(type == 'month'){
            $scope.endTime = date.format('yyyy-MM-dd');
            date.add('m', -1);
            $scope.startTime = date.format('yyyy-MM-dd');
        } else if(type == 'week'){
            $scope.endTime = date.format('yyyy-MM-dd');
            date.add('d', -7);
            $scope.startTime = date.format('yyyy-MM-dd');
        }

        $scope.queryList();
    };

    $('#day').datepicker().on('changeDate.datepicker.amui', function(event) {
        $scope.startTime = event.date.format('yyyy-MM-dd 00:00:00');
        $scope.endTime = event.date.format('yyyy-MM-dd 23:59:59');
        $(this).datepicker('close');
        $(this).val('day');
        $scope.queryList();
    });
});